package com.wzf.springdemo.main;


import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.Scanner;

public class MyBatisPlusGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw  new MybatisPlusException("请输入正确的" + tip + "！");
    }

	public static void main(String[] args) throws SQLException {
                //5. 整合配置
                AutoGenerator mpg = new AutoGenerator();

                //1. 全局配置
                GlobalConfig gc = new GlobalConfig();
                String projectPath = System.getProperty("user.dir");
                gc.setOutputDir(projectPath + "/src/main/java");
                //作者
                gc.setAuthor("wzf");
                //打开输出目录
                gc.setOpen(false);
                //xml开启 BaseResultMap
                gc.setBaseResultMap(true);
                //xml 开启BaseColumnList
                gc.setBaseColumnList(true);
                // 实体属性 Swagger2 注解
                gc.setSwagger2(true);

                mpg.setGlobalConfig(gc);

				//2. 数据源配置
                DataSourceConfig dsc = new DataSourceConfig();
                dsc.setUrl("jdbc:mysql://localhost:3306/ry-vue?"+
                        "useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia" +
                        "/Shanghai");
                dsc.setDriverName("com.mysql.cj.jdbc.Driver");
                dsc.setUsername("root");
                dsc.setPassword("123456");
                mpg.setDataSource(dsc);

            //4. 包名策略配置
            PackageConfig pkConfig = new PackageConfig();
            pkConfig.setParent("com.wzf.springdemo")
                    .setMapper("mapper")//dao
                    .setService("service")//servcie
                    .setController("controller")//controller
                    .setEntity("bean")
                    .setXml("mapper");//mapper.xml


        //3. 策略配置globalConfiguration中
//				StrategyConfig stConfig = new StrategyConfig();
                StrategyConfig strategy = new StrategyConfig();
                //数据库表映射到实体的命名策略
                strategy.setNaming(NamingStrategy.underline_to_camel);
                //数据库表字段映射到实体的命名策略
                strategy.setColumnNaming(NamingStrategy.no_change);
                //lombok模型
                strategy.setEntityLombokModel(true);
                //生成 @RestController 控制器
                strategy.setRestControllerStyle(true);
                strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
                strategy.setControllerMappingHyphenStyle(true);
                //表前缀
                strategy.setTablePrefix(pkConfig.getModuleName()+"_");

                mpg.setStrategy(strategy);
                mpg.setPackageInfo(pkConfig);

                        //6. 执行
                mpg.execute();
	}

}
